package com.eshop.controller.order;

import com.eshop.pojo.Good;
import com.eshop.pojo.Order;
import com.eshop.pojo.RespBean;
import com.eshop.pojo.RespPageBean;
import com.eshop.service.GoodService;
import com.eshop.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "待处理订单管理相关")
@RestController
@RequestMapping("/order/list")
public class OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    GoodService goodService;

    //分页获取订单列表
    @ApiOperation("分页获取待处理订单列表")
    @GetMapping("/")
    public RespPageBean getOrderListByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                          @RequestParam(defaultValue = "8") Integer size, Order order) {
        return orderService.getOrderListByPage(currentPage, size,order);
    }

    //分页获取订单列表
    @ApiOperation("分页获取当前顾客待处理订单列表")
    @GetMapping("/customer")
    public RespPageBean getOrderListByPageC(@RequestParam(defaultValue = "1") Integer currentPage,
                                           @RequestParam(defaultValue = "8") Integer size, Order order) {
        return orderService.getOrderListByPageC(currentPage, size,order);
    }

    @ApiOperation("卖家物流发货")
    @PutMapping("/delivered/{id}/{gid}")
    public RespBean Delivered(@PathVariable int id, @PathVariable int gid){
        //若发货成功则商品库存自动减1
        if (orderService.Delivered(id) == 1){
            Good good = goodService.queryGoodById(gid);
            good.setGCount(good.getGCount()-1);
            if (good.getGCount()==0)
            {
                goodService.recoverGood(gid);
                goodService.removeGood(gid);
            }
            goodService.updateById(good);
            return RespBean.success("发货成功！");
        }
        return RespBean.error("发货失败！");
    }

    @ApiOperation("订单交易成功")
    @PutMapping("/{id}/{gid}")
    public RespBean OrderSuccess(@PathVariable int id, @PathVariable int gid){
        //若交易成功则库存该商品库存自动减1
        if (orderService.OrderSuccess(id) == 1){
            Good good = goodService.queryGoodById(gid);
            good.setGCount(good.getGCount()-1);
            if (good.getGCount()==0)
            {
                goodService.recoverGood(gid);
                goodService.removeGood(gid);
            }
            goodService.updateById(good);
            return RespBean.success("订单交易成功！");
        }
        return RespBean.error("订单交易成功操作失败！");
    }

    @ApiOperation("订单交易失败")
    @PutMapping("/fail/{id}")
    public RespBean OrderFail(@PathVariable int id){
        if (orderService.OrderFail(id) == 1){
            return RespBean.success("该订单已取消！");
        }
        return RespBean.error("订单取消操作失败！");
    }

    @ApiOperation("添加订单")
    @PostMapping("/")
    public RespBean addOrder(@RequestBody Order order){
        if (orderService.addOrder(order) == 1){
            return RespBean.success("添加成功！");
        }
        return RespBean.error("添加失败！");
    }
}
